﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.BLL;
using GCELibrary.Current;

namespace ZF.WS.Person
{
    public partial class setinfo : ZF.BLL.Page.GMPage
    {
        [PageArgument(Key = "ot", DefaultValue = PageOperType.Edit)]
        public override PageOperType OperType
        {
            get
            {
                return base.OperType;
            }
            set
            {
                base.OperType = value;
            }
        }

        /// <summary>
        /// 1为中介，0为普通
        /// </summary>
        [PageArgument(Key = "m", DefaultValue = 0)]
        public int IsMedium { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {

            Response.Redirect("~/Person/expert/applye.aspx");
            return;


            if (!IsPostBack)
            {
                Edit();
            }
        }
        public void InitControls()
        {
            this.phIsMedium.Visible = this.IsMedium == 1;

            var sp = new house.Search();
            this.ddlArea.DataSource = sp.City.Select(p => new { Key = p.Name, Text = p.Name }); ;
            this.ddlArea.DataBind();
        }
        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                var member = dc.US_Member.FirstOrDefault(p => p.MemberId == this.CurrentUser.MemberId);
                if (member == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                InitControls();
                this.tbAddress.Text = member.Address;
                this.tbContact.Text = member.Contact;
                this.tbIDCard.Text = member.IDCard;
                this.tbPhone.Text = member.Phone;
                this.tbTel.Text = member.Tel;
                this.ddlJob.SelectedValue = member.Professional;
                this.tbQQ.Text = member.QQ;
                this.rblSex.SelectedValue = member.Sex.ToString();
                this.tbZipcode.Text = member.Zipcode;
                this.tbBD.Text = member.Birthday;
                this.tbCompany.Text = member.Company;
                this.tbSpecialty.Text = member.Specialty;
                this.tbPhoto.Text = member.Icon;
                this.tbBlog.Text = member.Blog;

                this.lvImgList.DataSource = member.Icon.EmptyString("").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(p => new
                    {
                        File = p
                    });
                this.lvImgList.DataBind();
             
            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var member = dc.US_Member.FirstOrDefault(p => p.MemberId == this.CurrentUser.MemberId);
                    if (member == null)
                        throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);


                    member.Blog = this.tbBlog.Text;
                    if (member.Blog.ToLower().IndexOf("http://") == -1)
                    {
                        member.Blog = "http://" + member.Blog;
                    }
                    member.Address = this.tbAddress.Text.EmptyString();
                    member.Contact = this.tbContact.Text.EmptyString();
                    member.IDCard = this.tbIDCard.Text.EmptyString();
                    member.Phone = this.tbPhone.Text.EmptyString();
                    member.Professional = this.ddlJob.SelectedValue.EmptyString();
                    member.QQ = this.tbQQ.Text.EmptyString();
                    member.Sex = this.rblSex.SelectedValue.Convert<int>();
                    member.Tel = this.tbTel.Text.EmptyString();
                    member.Zipcode = this.tbZipcode.Text.EmptyString();
                    member.Birthday = this.tbBD.Text;
                    member.Company = this.tbCompany.Text.EmptyString();
                    member.Specialty = this.tbSpecialty.Text;
                    var photoList = this.tbPhoto.Text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    photoList.AddRange(this.SWFUpLoad1.UpLoadFileInfos);
                    member.Icon = photoList.ConstituteString(";").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                        .ConstituteString(";");

     
                    try
                    {
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    string msgKey = "个人资料";

                    SiteInfo.InsertLog("修改" + "个人信息", null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功修改{0}", msgKey));

                    link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey), Request.Url.ToString());
                    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, "/Person/index.aspx").Alert();

                }


            }

        }


    }
}
